package com.dzl.voucherSecretGroup.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.config.database.Master;
import com.dzl.config.exception.CommonException;
import com.dzl.goods.pojo.WelfareGoods;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.util.EmptyUtils;
import com.dzl.voucherSecret.pojo.WelfareVoucherSecret;
import com.dzl.voucherSecret.pojo.dto.VoucherSecretGroupDto;
import com.dzl.voucherSecret.service.WelfareVoucherSecretService;
import com.dzl.voucherSecretGroup.dao.WelfareVoucherSecretGroupMapper;
import com.dzl.voucherSecretGroup.pojo.WelfareVoucherSecretGroup;
import com.dzl.voucherSecretGroup.pojo.query.VoucherSecretGroupQuery;
import com.dzl.voucherSecretGroup.pojo.vo.VoucherSecretGroupSellVo;
import com.dzl.voucherSecretGroup.pojo.vo.VoucherSecretGroupVo;
import com.dzl.voucherSecretGroup.service.WelfareVoucherSecretGroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 劵密分组表(WelfareVoucherSecretGroup)表服务实现类
 * @author caobucao
 * @date 2021-05-22 14:17:53
 */
@Service
public class WelfareVoucherSecretGroupServiceImpl extends BaseServiceImpl<WelfareVoucherSecretGroup> implements WelfareVoucherSecretGroupService {

    @Autowired
    private WelfareVoucherSecretGroupMapper mapper;
    @Autowired
    private WelfareVoucherSecretService welfareVoucherSecretService;
    @Autowired
    private WelfareGoodsService welfareGoodsService;

    @Override
    public PageInfo<VoucherSecretGroupVo> selectVoucherSecretGroupVoListPage(VoucherSecretGroupQuery query) {
        PageHelper.startPage(query);
        List<VoucherSecretGroupVo> list = mapper.selectVoucherSecretGroupVoList(query);
        Date nowDate = new Date();
        if(EmptyUtils.isNotEmpty(list)) {
            list.forEach(vo -> {
                List<WelfareGoods> goodsList = welfareGoodsService.selectRelateGoodsNameByGroupId(vo.getId());
                if(goodsList.size() > 0) {
                    vo.setIsRelateGoods("是");
                    StringBuffer relateGoodsName = new StringBuffer();
                    goodsList.forEach(goods -> {
                        relateGoodsName.append(goods.getGoodsName()).append(",");
                    });
                    if(relateGoodsName.length() > 0 && relateGoodsName.substring(relateGoodsName.length() - 1).equals(",")) {
                        relateGoodsName.deleteCharAt(relateGoodsName.length() - 1);
                    }
                    vo.setRelateGoodsName(relateGoodsName.toString());
                } else {
                    vo.setIsRelateGoods("否");
                }
                Date expiredTime = vo.getExpiredTime();
                //看是否已过期
                Integer isExpired = nowDate.getTime() > expiredTime.getTime() ? 1 : 0;
                vo.setIsExpired(isExpired);
                //看离过期是否只剩1天，给当前时间+1天再进行比较
                Calendar cal = Calendar.getInstance();
                cal.setTime(nowDate);
                cal.add(Calendar.DATE, 1);
                Date oneDayAfter = cal.getTime();
                Integer isOneDayExpired = oneDayAfter.getTime() > expiredTime.getTime() ? 1 : 0;
                vo.setIsOneDayExpired(isOneDayExpired);
            });
        }
        PageInfo<VoucherSecretGroupVo> page = new PageInfo<VoucherSecretGroupVo>(list);
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Master
    @Override
    public Integer insertVoucherSecretGroup(VoucherSecretGroupDto dto) {
        //先增加券密组
        //判断券密组名称是否已存在
        Example nameExample = new Example(WelfareVoucherSecretGroup.class);
        nameExample.createCriteria().andEqualTo("groupName", dto.getGroupName()).andEqualTo("status", 1);
        int countName = mapper.selectCountByExample(nameExample);
        if(countName > 0) {
            throw new CommonException("商品券密组名重复！");
        }
        WelfareVoucherSecretGroup welfareVoucherSecretGroup = dto.getWelfareVoucherSecretGroup();
        mapper.insertSelective(welfareVoucherSecretGroup);
        //添加券密
        //遍历前端传入的券密
        List<String> voucherSecretList = dto.getVoucherSecretList();
        List<WelfareVoucherSecret> list = new ArrayList<>();
        voucherSecretList.forEach(voucherSecret -> {
            WelfareVoucherSecret welfareVoucherSecret = new WelfareVoucherSecret();
            welfareVoucherSecret.setGroupId(welfareVoucherSecretGroup.getId());
            welfareVoucherSecret.setSecret(voucherSecret);
            welfareVoucherSecret.setExpiredTime(welfareVoucherSecretGroup.getExpiredTime());
            welfareVoucherSecret.setIsExpired(1); //0已过期，1未过期
            list.add(welfareVoucherSecret);
        });
        return welfareVoucherSecretService.insertList(list);
    }

    @Override
    public Integer deleteGroupById(Integer id, Integer updater) {
        //查看券密组是否已关联商品
        Integer countRelateGoods = mapper.selectCountRelateGoods(id);
        if(countRelateGoods > 0) {
            return 0;
        }
        //如果没有关联商品，删除券密组时同时删除券密组下的所有券密
        //先逻辑删除券密组下的所有未售出券密
        welfareVoucherSecretService.deleteVoucherSecretByGroupId(id);
        //逻辑删除券密组
        WelfareVoucherSecretGroup welfareVoucherSecretGroup = new WelfareVoucherSecretGroup();
        welfareVoucherSecretGroup.setId(id);
        welfareVoucherSecretGroup.setUpdateTime(new Date());
        welfareVoucherSecretGroup.setUpdater(updater);
        welfareVoucherSecretGroup.setStatus(0);
        return mapper.updateByPrimaryKeySelective(welfareVoucherSecretGroup);
    }

    @Override
    public VoucherSecretGroupSellVo getVoucherSecretGroupWithSellInfo(Integer voucherGroupId) {
        return mapper.getVoucherSecretGroupWithSellInfo(voucherGroupId);
    }

    @Override
    public Integer updateName(WelfareVoucherSecretGroup group) {
        String groupName = group.getGroupName();
        //判断券密组名称是否已存在
        Example nameExample = new Example(WelfareVoucherSecretGroup.class);
        nameExample.createCriteria().andEqualTo("groupName", groupName).andEqualTo("status", 1).andNotEqualTo("id", group.getId());
        int countName = mapper.selectCountByExample(nameExample);
        if(countName > 0) {
            throw new CommonException("商品券密组名已存在！");
        }
        return mapper.updateByPrimaryKeySelective(group);
    }

}
